package com.logistics.sys.interceptor;

import com.logistics.conf.Constants;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Mklaus on 15/8/16.
 */
public class AuthorizeInterceptor implements HandlerInterceptor {
    private List<String> admin_authorization;

    public void setAdmin_authorization(List<String> admin_authorization) {
        this.admin_authorization = admin_authorization;
    }

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        System.out.println("can intercepter. url = " + req.getRequestURI());
        String request_uri = new String(req.getRequestURI());

        for (String uri : admin_authorization){
            if (request_uri.equals(uri)){
                if (req.getSession().getAttribute(Constants.ADMIN_UID) != null){
                    return true;
                }else {
                    throw new AuthorizationException("in interceptor");
                }
            }
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
